/**

This file is part of MaCI/GIMnet.

MaCI/GIMnet is free software: you can redistribute it and/or modify it 
under the terms of the GNU Lesser General Public License as published 
by the Free Software Foundation, either version 3 of the License, or 
(at your option) any later version.

MaCI/GIMnet is distributed in the hope that it will be useful, but WITHOUT 
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public 
License for more details.

You should have received a copy of the GNU Lesser General Public 
License along with GIMnet. (See COPYING.LESSER) If not, see 
<http://www.gnu.org/licenses/>.

**/
#include "USB_Linux.hpp"
#include "ownutils.h"
#include "owndebug.h"
#define REQUEST_TYPE   0x00a1
#define REQUEST_VALUE  0x0300
#define REQUEST_TO     0x01f4
#define VENDORID       0x1130
#define PRODUCTID      0x0202
int main(void){
  // Init Debug lib
  debugInit();
  debugSetGlobalDebugLvl(3);
  unsigned int vendorId=  0x1130;
  unsigned int productId = 0x0202;

  CUSB_Linux usb;
  struct usb_device *devs[10];
  //Search for the "USB Panic button"
  int  numberOfDevices = usb.SearchForDevice(vendorId,productId,devs);
  dPrint(1,"numberOf devs %d",numberOfDevices);
  if(numberOfDevices > 0){

    //Open the first device which was found
    if(usb.Open(devs[0])){
      dPrint(1,"Opened succesfully");
    
      char buffer[8];
      *buffer = 0;
      while(!(*buffer)){
        //Send a control message
        usb.ControlMessage(buffer, 8,5000,
                           REQUEST_TYPE,
                           1,
                           REQUEST_VALUE,
                           0);
        
        ownSleep_ms(1);
      }
      
      dPrint(1,"Pressed");

      //Close the device
      usb.Close();
      dPrint(1,"Closed usb");
    }else{
      dPrint(1,"failed to Open");
      exit(1);
    }
  }
 
  
  exit(1);

}
